import { UserAuthGuard } from '@/common/auth/user-auth.guard';
import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Patch,
  Post,
  Query,
  UseGuards,
  Put, ParseUUIDPipe, Ip,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
  UserAdminIndexDto,
  UserAdminUpdateDto,
  UserCreateDto,
  UserLoginDto, UserMobileLoginWxDto, UserMobilePreLoginWxDto, UserResetPasswordDto,
  UserUpdateDto,
  UserUpdatePasswordDto,
  UserVerifyRegisterDto,
} from './user.dto';
import { UserService } from './user.service';
import { UserModel } from '@/module/user/user.model';
import { User } from '@/common/auth/user.decorator';
import {
  UserAdminIndexResult,
  UserLoginResult, UserPreLoginWxResult,
  UserShowMineResult,
  UserVerifyRegisterResult
} from "@/module/user/user.result";
import { AdminAuthGuard } from "@/common/auth/admin-auth.guard";
import { getPager } from "@/common/pager-response";
import { PageResult } from "common/interface";

@Controller('/v1/mobile/users')
@ApiTags('User_mobile')
export class UserV1MobileController {
  constructor(private readonly userService: UserService) {
  }

  @Get('/verify-register')
  @ApiOperation({ summary: '校验用户名、邮箱是否重复' })
  @ApiOkResponse({ type: UserVerifyRegisterResult })
  async verifyRegister(
    @Query() query: UserVerifyRegisterDto,
  ): Promise<UserVerifyRegisterResult> {
    const isExists = await this.userService.verifyRegister(query);
    return {
      isExists,
    };
  }

  @Post('/')
  @ApiOperation({ summary: '注册用户' })
  async create(
    @Body() body: UserCreateDto,
  ): Promise<void> {
    await this.userService.create(body);
    return;
  }

  @Post('/pre-login-wx')
  @ApiOperation({ summary: '微信预登录，根据token和jscode去查绑定帐号，返回token即为登陆成功，token为null即为未登录' })
  @ApiOkResponse({ type: UserPreLoginWxResult })
  async preLoginWx(
    @Body() body: UserMobilePreLoginWxDto,
  ): Promise<UserPreLoginWxResult> {
    const res = await this.userService.preLoginWx(body);
    return res;
  }

  @Post('/login-wx')
  @ApiOperation({ summary: '微信登录' })
  @ApiOkResponse({ type: UserLoginResult })
  async loginWx(
    @Body() body: UserMobileLoginWxDto,
  ): Promise<UserLoginResult> {
    const token = await this.userService.loginWx(body);
    return { token };
  }

  @Post('/logout')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '登出' })
  async logout(@Headers('token') token: string): Promise<void> {
    await this.userService.logout(token);
    return;
  }

  @Get('/mine')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '查询个人详情' })
  @ApiOkResponse({ type: UserShowMineResult })
  async showMine(@User() user: UserModel): Promise<UserShowMineResult | null> {
    const res = await this.userService.showMine(user.uuid);
    return res;
  }

  @Patch('/mine')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '更新个人信息' })
  async updateMine(
    @Body() body: UserUpdateDto,
    @User() user: UserModel,
  ): Promise<void> {
    await this.userService.updateMine(body, user.uuid);
    return;
  }

  @Put('/mine/password')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '更新密码' })
  async updateMinePassword(
    @Body() body: UserUpdatePasswordDto,
    @User() user: UserModel,
  ): Promise<void> {
    await this.userService.updateMinePassword(body, user.uuid);
    return;
  }

  @Post('/reset-password')
  @ApiOperation({ summary: '忘记密码' })
  async resetPassword(
    @Body() body: UserResetPasswordDto,
  ): Promise<void> {
    await this.userService.resetPassword(body);
    return;
  }
}
